package com.carey.demo.toast;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ToastDemo extends Activity implements OnClickListener {
	private final Handler handler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findAllView();
	}

	private void findAllView() {
		findViewById(R.id.btNormalToast).setOnClickListener(this);
		findViewById(R.id.btCustomPositionToast).setOnClickListener(this);
		findViewById(R.id.btPictureToast).setOnClickListener(this);
		findViewById(R.id.btCustomToast).setOnClickListener(this);
		findViewById(R.id.btOtherThreadToast).setOnClickListener(this);
	}

	private void backgroundThreadProcessing() {
		handler.post(doUpdateGUI);
	}

	// Runnable that executes the update GUI method.
	private Runnable doUpdateGUI = new Runnable() {
		public void run() {
			Context context = getApplicationContext();
			String msg = "I come form background thread!";
			int duration = Toast.LENGTH_LONG;
			Toast.makeText(context, msg, duration).show();
		}
	};

	public void onClick(View v) {
		Toast toast = null;
		int xOffset = 0;
		int yOffset = 0;

		switch (v.getId()) {
		case R.id.btNormalToast:
			Toast.makeText(getApplicationContext(), "Normal Toast",
					Toast.LENGTH_LONG).show();
			break;

		case R.id.btCustomPositionToast:
			toast = Toast.makeText(getApplicationContext(),
					"Custom Position Toast", Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, xOffset, yOffset);
			toast.show();
			break;

		case R.id.btPictureToast:
			toast = Toast.makeText(getApplicationContext(), "Picture Toast",
					Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, xOffset, yOffset);
			LinearLayout toastView = (LinearLayout) toast.getView();
			ImageView iv = new ImageView(getApplicationContext());
			iv.setImageResource(R.drawable.icon);
			toastView.addView(iv, 0);
			toast.show();
			break;

		case R.id.btCustomToast:
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.customtoastview,
					(ViewGroup) findViewById(R.id.toastrootview));
			((ImageView) layout.findViewById(R.id.image))
					.setImageResource(R.drawable.icon);
			((TextView) layout.findViewById(R.id.title)).setText("Title");
			((TextView) layout.findViewById(R.id.text)).setText("Content");
			toast = new Toast(getApplicationContext());
			xOffset = 10;
			yOffset = 20;
			toast.setGravity(Gravity.LEFT | Gravity.TOP, xOffset, yOffset);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();
			break;

		case R.id.btOtherThreadToast:
			new Thread(new Runnable() {
				public void run() {
					backgroundThreadProcessing();
				}
			}).start();
			break;
		}

	}
}